package cn.mrlong.keeplive.keeplive;

import android.app.Activity;
import android.content.Intent;

/**
 * Created by ly343 on 2018/11/27.
 */

public class InitSaveUtil {


    private Activity activity;

    public InitSaveUtil(Activity activity) {
        this.activity = activity;
    }

    //--------------------进程保活--------------------
    // JobService，执行系统任务
    private JobSchedulerManager mJobManager;

    public void startSaveMethod() {
       try {
           //启动系统任务
           //兼容android4
           if(Build.VERSION.SDK_INT>=21){
               mJobManager = JobSchedulerManager.getJobSchedulerInstance(activity);
               if (ServiceUtils.isServiceRunning(activity, "cn.mrlong.keeplive.keeplive.AliveJobService")) {

               } else {
                   mJobManager.startJobScheduler();
               }
           }

           if (ServiceUtils.isServiceRunning(activity, "cn.mrlong.keeplive.keeplive.DaemonService")) {

           } else {
               //启动前台Service
               Intent intent = new Intent();
               intent.setClass(activity, DaemonService.class);
               intent.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
               activity.startService(intent);
           }

           if (ServiceUtils.isServiceRunning(activity, "cn.mrlong.keeplive.keeplive.PlayerMusicService")) {

           } else {
               //启动音乐Service
               Intent intent = new Intent();
               intent.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
               intent.setClass(activity, PlayerMusicService.class);
               activity.startService(intent);
           }
       }catch (Exception e){
           e.printStackTrace();
       }
    }
}